package cn.dt70.controller.version;

import cn.dt70.pojo.AppVersion;
import cn.dt70.pojo.DevUser;
import cn.dt70.services.VersionService;
import cn.dt70.utils.Constrants;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/version/")
public class VersionController {

    @Resource
    private VersionService versionService;

    @RequestMapping("getByIdVersion")
    public String getByIdVersion(@RequestParam("id")int appinfoid, Model model){
        List<AppVersion> appVersionList =  versionService.getAllVersion(appinfoid,0);

        model.addAttribute("appVersionList",appVersionList);
        model.addAttribute("appId",appinfoid);
        return "developer/appversionadd";
    }

    @RequestMapping(value = "addVersion",method = RequestMethod.POST)
    public String addVersion(AppVersion appVersion, HttpSession session,
                             @RequestParam("a_downloadLink") MultipartFile multipartFile,Model model){
        String retCode = "F";
        String retMsg = "";


        //获取文件名
        String fileName = multipartFile.getOriginalFilename();

        //取后缀名
        String suffixName = FilenameUtils.getExtension(fileName);
        if("jpg".equals(suffixName) || "png".equals(suffixName)){
            File file = new File(Constrants.UPLOAD_DISK_PATH,fileName);

            try {
                if (!file.exists()){
                    file.createNewFile();
                }
                //写入到磁盘
                multipartFile.transferTo(file);
            } catch (IOException e) {
                //e.printStackTrace();
                System.out.println("上传失败。原因："+e.getMessage());
                retMsg = "上传失败，请重新上传！";
            }

        }else {
            retMsg = "上传的文件格式只能是.jpg或者.png";
        }

        DevUser devUser = (DevUser) session.getAttribute(Constrants.USER_SESSION);
        appVersion.setCreatedBy(devUser.getId());
        //加入图片相对路径
        appVersion.setApkLocPath(Constrants.UPLOAD_DISK_PATH+fileName);
        appVersion.setDownloadLink(Constrants.UPLOAD_PATH+fileName);
        appVersion.setApkFileName(fileName);

        retCode = "T";
        retMsg = "上传成功!";

        model.addAttribute("retCode",retCode);
        model.addAttribute("retMsg",retMsg);

        if(versionService.addNewVersion(appVersion)) {
            System.out.println("添加成功");
            //将新增版本信息写入appifo中（待加）
            return "dev/applist";
        }else {
            System.out.println("添加失败");
            return "developer/appversionadd";
        }
    }


    @RequestMapping("updByIdVersion")
    public String updByIdVersion(@RequestParam("vid")int vid,
                                 @RequestParam("aid")int aid,
                                 Model model){
        System.out.println(vid+","+aid);
        List<AppVersion> appVersion =  versionService.getAllVersion(aid,vid);
        model.addAttribute("appVersion",appVersion.get(0));

        return "developer/appversionmodify";
    }
    @RequestMapping(value = "updVersion",method = RequestMethod.POST)
    public String updVersion(AppVersion appVersion, HttpSession session,
                             @RequestParam("a_downloadLink") MultipartFile multipartFile,Model model){
        String retCode = "F";
        String retMsg = "";


        //获取文件名
        String fileName = multipartFile.getOriginalFilename();

        //取后缀名
        String suffixName = FilenameUtils.getExtension(fileName);
        if("jpg".equals(suffixName) || "png".equals(suffixName)){
            File file = new File(Constrants.UPLOAD_DISK_PATH,fileName);

            try {
                if (!file.exists()){
                    file.createNewFile();
                }
                //写入到磁盘
                multipartFile.transferTo(file);
            } catch (IOException e) {
                //e.printStackTrace();
                System.out.println("上传失败。原因："+e.getMessage());
                retMsg = "上传失败，请重新上传！";
            }

        }else {
            retMsg = "上传的文件格式只能是.jpg或者.png";
        }

        DevUser devUser = (DevUser) session.getAttribute(Constrants.USER_SESSION);
        appVersion.setCreatedBy(devUser.getId());
        //加入图片相对路径
        appVersion.setApkLocPath(Constrants.UPLOAD_DISK_PATH+fileName);
        appVersion.setDownloadLink(Constrants.UPLOAD_PATH+fileName);
        appVersion.setApkFileName(fileName);

        retCode = "T";
        retMsg = "上传成功!";

        model.addAttribute("retCode",retCode);
        model.addAttribute("retMsg",retMsg);

        if(versionService.updateUser(appVersion)) {
            System.out.println("修改成功");
            //将新增版本信息写入appifo中（待加）
            return "dev/applist";
        }else {
            System.out.println("修改失败");
            return "developer/appversionmodify";
        }

    }

}
